var service_user_id = getCookie("service_user_id");
$(function () {
    repairState(0,ALL_REPAIR);
})

! function($) {
    var $tab = $('#J_Tab');

    $tab.tab({
        nav: '.tab-nav-item',
        panel: '.tab-panel-item',
        activeClass: 'tab-active'
    });

//  $tab.find('.tab-nav-item').on('opened.ydui.tab', function(e) {
    	
//      console.log('索引：%s - [%s]已经打开了', e.index, $(this).text());
//  });
}(jQuery);

//获得维修人员全部工单
function repairState(index,flag) {
    var str = "";
    $(".tab-panel .tab-active").html("");
    if (flag == 0) {
    	Server.getFromService("/listRepairRecordAll",{"serviceUserId":service_user_id},function(result){

			if (result.data.length == 0) {
				str += "暂无工单";
			} else {
				$.each(result.data, function (i, data) {
	            	str += repairWork(data);
		        });	
			}
	      	$(".tab-panel .tab-active").append(str);
		});
    } else {
    	Server.getFromService("/listRepairRecordByRepairState",{"serviceUserId":service_user_id,"repairState":flag},function(result){
			if (result.data.length == 0) {
				str += "暂无工单";
			} else {
				$.each(result.data, function (i, data) {
		            str += repairWork(data);
		        });
			}
	        $(".tab-panel .tab-active").append(str);
	        
		});
    }
}

function repairWork(data) {

	var str = "<div class='weui-form-preview' style='margin-bottom: .2rem;'>" +
		"<a href='javascript:;' onclick='detailRepairRecord("+data.id+")'>" +
			"<div class='weui-form-preview__hd'>" +
				"<label class='weui-form-preview__label'>工单编号：<span>"+data.workOrderNo+"</span></label>"  +
				 "<em class='weui-form-preview__value'><span class='status'>"+stateToTxt(data.repairState)+"</span></em>";

		str +="</div>" +
			"<div class='weui-form-preview__bd'>" +
				"<div class='weui-form-preview__item'>" +
					"<label class='weui-form-preview__label' style='color:#ababab;'>客户信息</label>"+
					"<span class='weui-form-preview__value'>"+data.customerName+"	"+data.customerTel+"</span>"+
				"</div>"+
				"<div class='weui-form-preview__item'>"+
					"<label class='weui-form-preview__label' style='color:#ababab;'>产品名称</label>"+
					"<span class='weui-form-preview__value'>"+data.productName+"</span>"+
				"</div>";
		if (data.appointmentTime != "") {
			str += "<div class='weui-form-preview__item'>"+
					"<label class='weui-form-preview__label' style='color:#ababab;'>预约上门时间</label>"+
					"<span class='weui-form-preview__value'>"+data.appointmentTime+"</span>"+
				"</div>";
		}
		str +=	"</div>"+
		"</a>";
		
		if (data.repairState == REPAIR_STATE_AUDIT ) {
			str += "<em class='weui-form-preview__value'><span class='status'><button class = 'btn' onclick = 'detailRepairRecord("+data.id+")'>工单详情</button><button class = 'btn' onclick = 'appointment("+data.id+")'>预约上门</button></span></em>";
		} else if (data.repairState == REPAIR_STATE_BESPEAK) {
			str += "<em class='weui-form-preview__value'><span class='status'><button class = 'btn' onclick = 'detailRepairRecord("+data.id+")'>工单详情</button><button class = 'btn' onclick = 'feedBack("+JSON.stringify(data)+")'>提交反馈</button></span></em>";
		} else if (data.repairState == REPAIR_STATE_FEEDBACK) {
			str += "<em class='weui-form-preview__value'><span class='status'><button class = 'btn' onclick = 'detailRepairRecord("+data.id+")'>工单详情</button><button class = 'btn' onclick = 'sign("+data.id+")'>签名确认</button></span></em>";		
		} else if (data.repairState == REPAIR_STATE_SIGN) {
			str += "<em class='weui-form-preview__value'><span class='status'><button class = 'btn' onclick = 'detailRepairRecord("+data.id+")'>工单详情</button></span></em>";
		} else {
			str += "<em class='weui-form-preview__value'><span class='status'><button class = 'btn' onclick = 'detailRepairRecord("+data.id+")'>工单详情</button></span></em>";
		}
		str += "</div>";
    return str;
}


